package net.xi.dmhome.config;

import net.xi.dmhome.config.realms.CustomerRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: jxhu
 * @Description:用于整合shiro框架的类
 * @Date Created in 2021-02-03 11:47
 * @Modified By:
 */

@Configuration
public class ShiroConfig {

    /* @Description: 创建shiroFilter,@Param: 负责拦截所有请求 */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        //给filter设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);


        //配置系统受限资源
        //拦截
        Map<String,String> filterMap = new LinkedHashMap<>();
        filterMap.put("/manage","authc");
        filterMap.put("/login","anon");
//        filterMap.put("/manage","roles[root]");css
//fonts
//img
//js
//favicon.ico
//index.html
        //设置登录的请求
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        System.out.println("Shiro拦截器工厂类注入成功");
        return  shiroFilterFactoryBean;
    }
    /* @Description: 创建安全管理器,@Param:  */
    @Bean
    public DefaultWebSecurityManager getdefaultWebSecurityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();

        defaultWebSecurityManager.setRealm(getrealm());


        return defaultWebSecurityManager;
    }

    /* @Description: 创建自定义realm,@Param:  */
    @Bean
    public Realm getrealm(){
        CustomerRealm customerRealm = new CustomerRealm();
        return customerRealm;
    }
}
